<!-- 欠料处理弹窗 -->
<template>
  <el-dialog
    custom-class="handleWasteDialog"
    append-to-body
    :close-on-click-modal="false"
    title=" 欠料处理"
    :visible.sync="handleWasteDialog.visible"
    v-if="handleWasteDialog.visible"
    width="80%"
    top="7vh"
    @close="close()"
  >
    <el-tabs type="border-card" style="margin: 10px 0">
      <el-tab-pane label="普通货架物料">
        <avue-form ref="handleWasteForm" v-model="handleWasteForm" :option="handleWasteOption"> </avue-form>
        <avue-crud
          :option="handleWasteDialog.option"
          :table-loading="handleWasteDialog.loading"
          :data="handleWasteDialog.data"
          v-model="handleWasteDialog.form"
          ref="handleWasteTableCrud"
          @selection-change="selectionChange"
        >
          <!-- 调整后应发数量 -->
          <template slot-scope="{ row, index }" slot="tzQuantity">
            <el-input-number
              controls-position="right"
              size="mini"
              v-model="row.tzQuantity"
              :min="Number(row.originalQtyTotal)"
              :max="Number(row.quantity - 1)"
              :precision="0"
              @change="(value) => changeTzQuantity(value, index, row)"
            ></el-input-number>
          </template>
        </avue-crud>
      </el-tab-pane>
      <el-tab-pane label="智能料架物料"> </el-tab-pane>
    </el-tabs>
    <div slot="footer" class="dialog-footer">
      <el-button v-preventReClick type="primary" size="small" @click="saveAddCache" icon="el-icon-circle-plus-outline">保存</el-button>
      <el-button @click="cancelAddData" size="small" icon="el-icon-circle-close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateOverList, getOverListByPartNo } from '@/api/warehouse/preMaterialList';

export default {
  name: 'handleWasteDialog',
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      handleWasteDialog: {
        visible: false,
        loading: false,
        data: [],
        form: {},
        option: {
          tip: false,
          searchShow: true,
          searchSpan: 4,
          searchMenuSpan: 6,
          dialogClickModal: false,
          height: '400',
          border: true,
          addBtn: false,
          editBtn: false,
          viewBtn: false,
          cancelBtn: false,
          saveBtn: false,
          selection: true,
          menu: false,
          page: false,
          index: true,
          indexLabel: '序号',
          refreshBtn: false,
          expandBtn: false,
          columnBtn: false,
          column: [
            {
              label: '备料单号',
              prop: 'sheetNo',
              overHidden: true
            },
            {
              label: '领料单号',
              prop: 'originalSheetNo',
              overHidden: true
            },
            {
              label: '单位',
              prop: 'uom'
            },
            {
              label: '需求数量',
              prop: 'originalQtyTotal'
            },
            {
              label: '最小包规数量',
              prop: 'mpq'
            },
            {
              label: '应发数量',
              prop: 'quantity'
            },
            {
              label: '超发数量',
              prop: 'overshipmentQuantity'
            },
            {
              label: '推荐批次',
              prop: 'recommendLotNo',
              overHidden: true
            },
            {
              label: '推荐包装',
              prop: 'recommendPack',
              overHidden: true
            },
            {
              label: '调整后应发数量',
              width: 160,
              prop: 'tzQuantity'
            },
            {
              label: '调整后超发数量',
              prop: 'tzOvershipmentQuantity'
            },
            {
              label: '拆出数量',
              prop: 'ccQuantity'
            }
          ]
        }
      },
      rowIndex: null,
      selectionList: [],
      handleWasteForm: {},
      handleWasteOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '物料编码',
            prop: 'partNo',
            disabled: true,
            span: 8
          },
          {
            label: '物料名称',
            prop: 'partName',
            disabled: true,
            span: 8
          },
          {
            label: '物料型号',
            prop: 'partModel',
            disabled: true,
            span: 8
          },
          {
            label: '需求数量',
            prop: 'originalQtyTotal',
            disabled: true,
            span: 8
          },
          {
            label: '应发数量',
            prop: 'quantity',
            disabled: true,
            span: 8
          },
          {
            label: '欠料数量',
            prop: 'shortageQuantity',
            disabled: true,
            span: 8
          }
        ]
      }
    };
  },
  watch: {
    visible: {
      handler(value) {
        this.handleWasteDialog.visible = value;
      }
    }
  },
  methods: {
    getData(partNo, index) {
      this.selectionList = [];
      this.rowIndex = index;
      this.handleWasteDialog.loading = true;
      getOverListByPartNo(partNo).then((res) => {
        const data = res.data.data;
        data.forEach((item) => {
          if (item.sheetRecommendList.length > 0) {
            let recommendPack = item.sheetRecommendList.map((list) => list.recommendDataDTO && list.recommendDataDTO.recommendPacks);
            let recommendLotNo = item.sheetRecommendList.map((list) => {
              if (list.recommendDataDTO && list.recommendDataDTO.recommendLotNo) {
                return list.recommendDataDTO.recommendLotNo + '(' + list.recommendDataDTO.recommendLotQuantity + ')';
              }
            });
            // item.recommendLotNo = [...new Set(recommendLotNo)].join(',');
            item.recommendLotNo = recommendLotNo.join(',');
            // item.recommendPack = [...new Set(recommendPack)].join(',');
            item.recommendPack = '';
          } else {
            item.recommendLotNo = '';
            item.recommendPack = '';
          }
          item.tzQuantity = Number(item.originalQtyTotal);
          item.tzOvershipmentQuantity = Number(item.tzQuantity || 0) - Number(item.originalQtyTotal);
          // item.ccQuantity = Number(item.mpq || 0) - Number(item.quantity);
        });
        this.handleWasteDialog.data = data;
        this.handleWasteDialog.loading = false;
      });
    },
    close() {
      this.handleWasteDialog.visible = false;
      this.$emit('closeHandleDialog');
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    saveAddCache() {
      if (this.selectionList.length == 0) {
        this.$message.warning('欠料处理数据不能为空，请重新选择后保存！');
        return false;
      }
      let result = structuredClone(this.selectionList);
      result.forEach((item) => {
        item.quantity = item.tzQuantity + '';
      });
      console.log('result', result);
      updateOverList(result)
        .then((res) => {
          let data = res.data.data;
          this.handleWasteDialog.visible = false;
          if (data[0]) {
            this.$emit('closeHandleDialog', data[0], this.rowIndex);
          }
        })
        .catch((err) => {});
    },
    cancelAddData() {
      this.handleWasteDialog.visible = false;
      this.$emit('closeHandleDialog');
    },
    changeTzQuantity(value, index, row) {
      row.tzQuantity = value;
      row.tzOvershipmentQuantity = Number(row.tzQuantity) - Number(row.originalQtyTotal);
      // row.ccQuantity = Number(row.mpq) - Number(row.quantity);
      this.handleWasteDialog.data.splice(index, 1, row);
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .handleWasteDialog {
  .avue-crud__menu {
    display: none;
  }
  .ruleRowForm {
    margin-bottom: 5px;
    .el-radio {
      margin-right: 10px;
    }
    .header-icon {
      margin-right: 10px;
    }
  }
}
</style>
